package com.aj.service.impl;

import com.aj.entity.Comment;
import com.aj.mapper.CommentMapper;
import com.aj.service.CommentService;
import com.aj.util.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2020-08-03
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

	@Autowired
	CommentMapper cm;
	
	@Override
	public Result getCommentList(Comment c) {
		// TODO Auto-generated method stub
		System.out.println(c.getAid());
		try {
			Iterator<Comment> i=cm.getList(c).iterator();
			LinkedHashMap<Long,List<Comment>> hm=new LinkedHashMap<Long,List<Comment>>();
			while(i.hasNext())
			{
				Comment ct=i.next();
				if(ct.getFloor()==0)
					hm.put(ct.getCid(), new ArrayList<Comment>() {{add(ct);}});
				else
				{
					if(hm.containsKey(ct.getFloor()))
						hm.get(ct.getFloor()).add(ct);
					else
						System.out.println(ct.getCid()+"号评论找不到楼层");
				}
			}
			return new Result(true,"成功辽",200,new ArrayList<List<Comment>>(hm.values()));
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
		
	}

	@Override
	public Result addComment(Comment c) {
		// TODO Auto-generated method stub
		try {
			cm.addComment(c);
			return new Result(true,"成功辽",200,null);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
	}

	@Override
	public Result deleteComment(Comment c) {
		// TODO Auto-generated method stub
		try {
			cm.deleteById(c.getCid());
			return new Result(true,"成功辽",200,null);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new Result(false,"失败辽",500,null);
		}
		
	}

}
